const express = require('express');
const router = express.Router();
const bodyParser = require('body-parser');
const managerSQL = require('../../db/managerSQL')
const loginSQL = require('../../db/loginSQL')

router.post('/setstatus', bodyParser.json(), (req, res) => {
    // 检查用户ID是否为空
    if (!req.body["userID"]
        || typeof req.body["userID"] != "number") {
        res.json({
            err: 1,
            message: '用户名为空'
        })
    } else if (req.body["managerID"] == undefined
        || req.body["managerID"] == null
        || typeof req.body["managerID"] != "number") {
        res.json({
            err: 1,
            message: '管理员id为空'
        })
    } else if (req.body["operation"] == undefined
        || req.body["operation"] == null
        || typeof req.body["operation"] != "boolean") {
        res.json({
            err: 1,
            message: '操作为空'
        })
    } else {
        let userID = req.body["userID"];
        let managerID = req.body["managerID"];
        let operation = req.body["operation"]
        loginSQL.getByID(managerID, (result) => {
            var userStatus = result[0].userStatus
            var userPower = result[0].userPower
            //判断管理员权限
            if (userPower == 0) {
                res.json({
                    err: 2,
                    message: '权限不足',
                    userPower: 0
                })
            } else if (userStatus == 0) {
                res.json({
                    err: 3,
                    message: '用户被封禁'
                })
            } else {
                if (!operation) {
                    managerSQL.setStatusBlock(userID, (block) => {
                        res.json({
                            err: 200,
                            message: block
                        })
                    })
                }
                else {
                    managerSQL.setStatusUnblock(userID, (unblock) => {
                        res.json({
                            err: 200,
                            message: unblock
                        })
                    })
                }
            }
        })
    }
})

module.exports = router;